package com.lagou.rpc.api.impl;

import cn.hutool.core.map.MapUtil;
import com.lagou.rpc.api.LoadBalanceStrategy;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @param
 * @author oyz
 * @version 1.0
 * @description: TODO
 * @date 2022/3/1 11:05
 */
public abstract class AbstractLoadBalance implements LoadBalanceStrategy {


    @Override
    public String route(Map<String, String> clientPool, String serviceClassName) {
        // 判断是否有可以连接的地址
        if (MapUtil.isEmpty(clientPool)){
            return null;
        }
        List<String> address = new CopyOnWriteArrayList<>();
        for (String key:clientPool.keySet()){
            if(key.startsWith(serviceClassName)){
                address.add(clientPool.get(key));
            }
        }


        return doSelect(address);
    }
    /**
     * @description: TODO 负载方法
     * @author oyz
     * @param address 路由地址
     * @date 2022/3/1 11:38
     * @version 1.0
     */
    protected abstract String doSelect(List<String> address);
}
